<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    :title="t('保养历史记录')"
    :width="1000"
    :showFooter="false"
  >
    <BasicTable @register="registerTable" />
  </BasicDrawer>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { BasicDrawer, useDrawer } from '/@/components/Drawer';
  import { asdMoldKeepFindHisData } from '/@/api/mold/keep/asdMoldKeep';

  const { t } = useI18n('mold.repair.asdMoldUpdate');

  const tableColumns: BasicColumn[] = [
    {
      title: t('保养内容'),
      dataIndex: 'keepDesc',
      key: 'keepDesc',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('保养时长(小时)'),
      dataIndex: 'keepTime',
      key: 'keepTime',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('送保人'),
      dataIndex: 'createByName',
      key: 'createByName',
      width: 120,
      align: 'left',
    },
    {
      title: t('模具保养人'),
      dataIndex: 'empUser.userName',
      key: 'empUser.userName',
      width: 120,
      align: 'left',
    },
    {
      title: t('备注信息'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const [registerDrawer, { openDrawer }] = useDrawer();

  // 存储查询参数
  const queryParams = ref<Recordable>({});

  const [registerTable, { reload }] = useTable({
    api: asdMoldKeepFindHisData,
    // 将分页参数与业务查询参数合并
    beforeFetch: (p) => ({ ...p, ...queryParams.value }),
    // 明确响应字段映射（与项目默认一致），避免适配错误
    fetchSetting: {
      pageField: 'pageNo',
      sizeField: 'pageSize',
      listField: 'list',
      totalField: 'count',
    },
    columns: tableColumns,
    showTableSetting: false,
    useSearchForm: false,
    canResize: true,
    showIndexColumn: true,
    indexColumnProps: {
      title: t('序号'),
      width: 60,
    },
  });

  function open(record: Recordable) {
    // 保存查询参数
    queryParams.value = {
      moldCode: record.moldCode,
      lastNum: record.lastNum,
      status: ' ',
    };
    // 重新加载表格数据
    reload();
    openDrawer(true, record);
  }

  defineExpose({
    open,
  });
</script>

<style scoped>
  .history-stats {
    display: flex;
    gap: 16px;
    padding: 8px 12px;
    background: var(--ant-primary-1, #f5f7fa);
    border: 1px solid #e5e7eb;
    border-radius: 4px;
    margin-bottom: 12px;
  }
  .stat-item {
    display: flex;
    align-items: center;
    gap: 6px;
  }
  .stat-label {
    color: #666;
  }
  .stat-value {
    font-weight: 600;
    color: #111;
  }
</style>
